import { _decorator, Component, Node, Prefab, resources } from 'cc';
import SUI from '../../../sgame/ui/SUI';
import { SGame } from '../../../sgame/SGame';
import { UIHintLayer } from './hint/UIHintLayer';
import { llog } from '../../../sgame/logger/Logger';
import { UIDialogLayer } from './dialog/UIDialogLayer';
import { UIWaitLayer } from './wait/UIWaitLayer';
import { UILoadingLayer } from './loading/UILoadingLayer';
import ResLoader from '../../../sgame/resource/ResLoader';
import { event_dispatch } from '../../../sgame/event/EventManager';
import { UIMask } from 'db://assets/sgame/comopnent/UIMask';
const { ccclass, property } = _decorator;

@ccclass('UIRoot')
export class UIRoot extends Component {
    @property({
        type: Node,
        tooltip: "UI根节点"
    })
    public guiRoot: Node;

    @property({
        type: Node,
        tooltip: "加载根节点"
    })
    public loadingRoot: Node;

    @property({
        type: Node,
        tooltip: "对话框根节点"
    })
    public dialogRoot: Node;

    @property({
        type: Node,
        tooltip: "等待根节点"
    })
    public waitRoot: Node;

    @property({
        type: Node,
        tooltip: "提示根节点"
    })
    public hintRoot: Node;

    @property(Prefab)
    private maskPrefab: Prefab;

    async onLoad() {
        if (SGame.HasInited) {
            this.init();
            return;
        }

        let config = await resources.loadJsonSync("config/config");
        llog("game config", config);
        SGame.Init(config);
        this.init();
    }

    async init() {
        // root
        SUI.UIRoot = this.node;
        this.guiRoot = this.guiRoot ?? this.node.Child('gui');
        this.loadingRoot = this.loadingRoot ?? this.node.Child('loading');
        this.dialogRoot = this.dialogRoot ?? this.node.Child('dialog');
        this.waitRoot = this.waitRoot ?? this.node.Child('wait');
        this.hintRoot = this.hintRoot ?? this.node.Child('hint');

        SUI.GUIRoot = this.guiRoot;
        SUI.LoadingRoot = this.loadingRoot;
        SUI.DialogRoot = this.dialogRoot;
        SUI.WaitRoot = this.waitRoot;
        SUI.HintRoot = this.hintRoot;

        if (this.maskPrefab == null) {
            this.maskPrefab = await ResLoader.LoadAssetSync('resources/prefabs/mask');
        }

        // mask
        SUI.Mask = (parent?: Node) => {
            const mask = SUI.InitPrefab(this.maskPrefab, parent ?? SUI.GUIRoot);
            return mask.Component(UIMask);
        }

        // loading
        let loadingLayer = this.loadingRoot.getComponent(UILoadingLayer);
        SUI.Loading = loadingLayer.loading.bind(loadingLayer);

        // dialog
        let dialogLayer = this.dialogRoot.getComponent(UIDialogLayer);
        SUI.Dialog = dialogLayer.show.bind(dialogLayer);

        // wait
        let waitLayer = this.waitRoot.getComponent(UIWaitLayer);
        SUI.Wait = waitLayer.show.bind(waitLayer);

        // hint
        let hintLayer = this.hintRoot.getComponent(UIHintLayer);
        SUI.Hint = hintLayer.show.bind(hintLayer);
        event_dispatch("uiroot::init_success");
        this.run();
    }

    run(): void { }
}

export const UIRootInitSuccess = "uiroot::init_success";